﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProjectLibrary;

namespace XNAProject
{
    public sealed class OptionsMenuState : BaseGameState, IOptionsMenuState
    {
        private GraphicsDeviceManager graphics;
        private SpriteFont font;
        private Texture2D background;
        private Vector2 resolution;

        private int selected;

        private string[] entries = 
            {
                "Windowed Mode",
                "Resolution",
                "",
                "Apply Changes",
                "Back"               
            };  

        public OptionsMenuState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IOptionsMenuState), this);                                          //register service
            graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));

            resolution = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            entries[1] = resolution.X.ToString() + " x " + resolution.Y.ToString();        
        }
        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.Start, Keys.Escape)                                                 //input handling
                || Input.WasPressed(0, Buttons.Start, Keys.Enter)
                || Input.WasPressed(0, Buttons.Start, Keys.Space))
            {
                //GameManager.ChangeState(OurGame.StartMenuState.Value);
            }

            if (Input.WasPressed(0, Buttons.DPadUp, Keys.Up))                                               //input handling
            {
                selected--;
            }
            if (Input.WasPressed(0, Buttons.DPadDown, Keys.Down))
            {
                selected++;
            }

            if (selected < 0)                                                                               //loop
                selected = entries.Length - 1;
            if (selected == entries.Length)
                selected = 0;

            if (Input.WasPressed(0, Buttons.Start, Keys.Enter) ||
                (Input.WasPressed(0, Buttons.A, Keys.Space)))
            {                                                                                               //handle se lection
                switch (selected)
                {
                    case 0:                                                                                 //Join Game
                        {
                            if (entries[0] == "Windowed Mode")
                            {
                                entries[0] = "Full Screen Mode";
                                graphics.IsFullScreen = true;
                            }
                            else
                            {
                                entries[0] = "Windowed Mode";
                                graphics.IsFullScreen = false;
                            }
                            break;
                        }
                    case 1:                                                                                 //Create Game
                        {
                            if (entries[1] == "1920 x 1080")
                            {
                                graphics.PreferredBackBufferWidth = 1680;
                                graphics.PreferredBackBufferHeight = 1024;
                            }
                            else if (entries[1] == "1680 x 1024")
                            {
                                graphics.PreferredBackBufferWidth = 1280;
                                graphics.PreferredBackBufferHeight = 1024;
                            }
                            else if (entries[1] == "1280 x 1024")
                            {
                                graphics.PreferredBackBufferWidth = 1024;
                                graphics.PreferredBackBufferHeight = 786;
                            }
                            else if (entries[1] == "1024 x 786")
                            {
                                graphics.PreferredBackBufferWidth = 800;
                                graphics.PreferredBackBufferHeight = 600;
                            }
                            else if (entries[1] == "800 x 600")
                            {
                                graphics.PreferredBackBufferWidth = 1920;
                                graphics.PreferredBackBufferHeight = 1080;
                            }
                            resolution = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
                            entries[1] = resolution.X.ToString() + " x " + resolution.Y.ToString(); 
                            break;
                        }
                    case 3:
                        {
                            graphics.ApplyChanges();
                            break;
                        }
                    case 4:                                                                                 //Exit
                        {
                            GameManager.ChangeState(OurGame.StartMenuState.Value);
                            break;
                        }
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 pos = new Vector2((GraphicsDevice.Viewport.Width - background.Width) / 2,
                (GraphicsDevice.Viewport.Height - background.Height) / 2);
            Vector2 position = new Vector2(pos.X + 140, pos.Y + background.Height / 2);

            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.Draw(background, pos, Color.White);

            for (int i = 0; i < entries.Length; i++)
            {
                Color color;
                float scale;

                if (i == selected)
                {
                    // The selected entry is yellow, and has an animating size.
                    double time = gameTime.TotalGameTime.TotalSeconds;

                    float pulsate = (float)Math.Sin(time * 12) + 1;

                    color = Color.White;
                    scale = 1 + pulsate * 0.05f;
                }
                else
                {
                    // Other entries are white.
                    color = Color.Blue;
                    scale = 1;
                }
                // Draw text, centered on the middle of each line.
                Vector2 origin = new Vector2(0, font.LineSpacing / 2);
                Vector2 shadowPosition = new Vector2(position.X - 2, position.Y - 2);

                //Draw Shadow
                OurGame.spriteBatch.DrawString(font, entries[i],
                    shadowPosition, Color.Black, 0, origin, scale, SpriteEffects.None, 0);
                //Draw Text
                OurGame.spriteBatch.DrawString(font, entries[i],
                    position, color, 0, origin, scale, SpriteEffects.None, 0);

                position.Y += font.LineSpacing;
            }

            OurGame.spriteBatch.DrawString(font, "OptionsMenuState \n press Esc or Enter or Space for StartMenuState", new Vector2(20, 20), Color.White);

            OurGame.spriteBatch.End();                                                                          //simple text draw
            base.Draw(gameTime);
        }

        //protected override void StateChanged(object sender, EventArgs e)
        //{
        //    base.StateChanged(sender, e);

        //    if (GameManager.State != this.Value)                                                            //event toggle visible
        //        Visible = true;
        //}

        protected override void LoadContent()
        {
            background = Content.Load<Texture2D>(@"Textures\Menu\optionMenu");
            font = Content.Load<SpriteFont>(@"Fonts\menu");                                              //load font
            base.LoadContent();
        }
    }
}
